<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="div">
    <span>
      <span></span>
    </span>
    <p>
    </p>
      <div><p></p></div>
  </div>
  <script>
    // 将dom转为json
    function dom2Json(dom){ 
      let obj = {}
      obj.tag = dom.tagName
      obj.children = []
      dom.childNodes.forEach(child => {
        if(child.nodeType != 3){
          obj.children.push(dom2Json(child))
        }
      });
      return obj
    }
    let dom = document.getElementById("div")
    console.log(dom)
    console.log(dom2Json(dom))
  </script>
</body>
</html>